import {observable, action, makeAutoObservable} from 'mobx';
import { makePersistable } from 'mobx-persist-store'
import AsyncStorage from "@react-native-async-storage/async-storage";

class RootStore {
    // observable 表示数据可监控,表示是全局数据
    // action 表示行为
    constructor() {
      // 使用 makeObservable mobx6.0 才会更新视图
      makeAutoObservable(this);
      makePersistable(this,{
        name: 'USER_DATA',
        properties: ['mobile','token','userId','info'],
        storage: AsyncStorage
      })
    }
    // 手机号码
    @observable mobile = '';
    // token
    @observable token = '';
    // 用户唯一id
    @observable userId = '';

    @observable info = {}
  
    @action setUserInfo(mobile, token, userId) {
      this.mobile = mobile;
      this.token = token;
      this.userId = userId;
    }

  @action setInfo(info = {}){
    this.info = {...info}
  }

    @action updateInfo(info = {}){
      this.info = { ...this.info, ...info }
    }
  
    // 清除信息
    @action clearUserInfo() {
      this.mobile = '';
      this.token = '';
      this.userId = '';
      this.info = {}
    }
  }
  
  export default new RootStore();